<?php

namespace teamug;

/*
 * PHP's mysqli class with added functionality
 * New methods for things we devs commonly do, and a little integration with teamug
 */

class Mysqli extends \mysqli {

  private static $connections;
  
  
  public static function getConnection($name) {
    
    if (!\array_key_exists($name, self::$connections)) {
      trigger_error("Connection name '$name' has not been declared", E_USER_WARNING);
      return null;
    }
    
    if (\is_object(self::$connections[$name]['connection'])) {
      return self::$connections['connection'][$name]; 
    }

    $arr = self::$connections[$name];
    $arr['connection'] = new \mysqli($arr['host'], $arr['username'], $arr['password'], $arr['database']);
    self::$connections[$name] = $arr;
    
    return self::$connections[$name]['connection'];
  }
  
  
  
  public static function declareConnection($name, $host, $username,$password, $database) {
    $arr = array();
    $arr['host'] = $host;
    $arr['username'] = $username;
    $arr['password'] = $password;
    $arr['database'] = $database;
    $arr['connection'] = null;
    self::$connections[$name] = $arr;
  }

  
  
  
  /**
   * Populate an array of DataStructure from an sql command
   * Note: DataStructure or subclass thereof
   * 
   * @param string $sql Sql command, with field names matching the DataStructure's properties
   * @param string $class Name of the DataStructure class
   * @param key $key Field to use as the key of the returned array, or null if a numerically keyed array
   * @return array Array of freshly instantiated DataStructure objects
   */
  public function populateDataStructures($sql, $class, $key=null) {
    
    $retval = array();
    
    $result = $this->query($sql);
    
    while($row = $result->fetch_assoc()) {
      if (\is_null($key)) {
        $retval[] = new $class($row);
      } else {
        $retval[$row[$key]] = new $class($row);
      }
        
    }
  
    return $retval;
  }
  
  
  /**
   * Populate a DataStructure from an sql command
   * Note: DataStructure or subclass thereof
   * 
   * @param string $sql Sql command, with field names matching the DataStructure's properties
   * @param string $class Name of the DataStructure class
   * @return object The new object
   */
  public function populateDataStructure($sql, $class) {
  
    $result = $this->query($sql);
    return new $class($result->fetch_assoc());
  }

  
  
  /**
   * Convert the first field of an SQL command into a numerically indexed array
   * 
   * @param string $sql SQL command
   * @return array Values of the SQL commands first field
   */
  public function getColumn($sql) {
    $retval = array();
    $result = $this->query($sql);
      while($row = $result->fetch_assoc()) {
        $retval[] = $row[0];
      }
      return $retval;
  }
  
  
  /**
   * Convert first and second field of an SQL command into an assoc array
   * 
   * @param string $sql SQL command of two columns
   * @return array Where the key is column one of the SQL command, and value is column two
   */
  public function getAssocArray($sql) {
    $retval = array();
    $result = $this->query($sql);
      while($row = $result->fetch_assoc()) {
        $retval[$row[0]] = $row[1];
      }
      return $retval;
  }
  
  /**
   * Get a single value from an SQL command
   * 
   * @param string $sql SQL command
   * @return string type The value in the first column of the first row
   */
  public function getScalar($sql) {
    $result = $this->query($sql);
    $row = $result->fetch_array();
    return $row[0];
  }
  
  /**
   * Convert SQL result into an HTML table
   * Useful for debugging
   * 
   * @param string $sql SQL command
   * @return string An HTML <table>
   * 
   */
  public function getHtmlTable($sql) {
    $html = '<table>';
    
    $result = $this->query($sql);
    
    $row = $result->fetch_assoc();
    
    $html.= "\n<tr><th>".implode('</th/><th>', \array_keys($row)).'</th></tr>';
    
    do {
      $html.= "\n<tr><td>".implode('</td/><td>', \array_values($row)).'</td></tr>';
    } while($row = $result->fetch_assoc());
    $html.= "\<table>";
    
    return $html;
  }
  
}

?>
